﻿namespace EFS.RC.CC.Srv
{
	static class Program
	{
		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		static void Main()
		{
			// start app
			System.AppDomain.CurrentDomain.UnhandledException += new System.UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

			// read command line
			System.String[] args = System.Environment.GetCommandLineArgs();
			if (args.Length > 1)
			{
				if (args[1] == "console")
				{
					RunApp();
				}
				else
				{// say 
					System.Console.WriteLine("Use \"console\" to run as Console Application or run as service");
					System.Console.ReadLine();
				}
			}
			else
			{
				System.ServiceProcess.ServiceBase.Run(new System.ServiceProcess.ServiceBase[] { new EFS.RC.CC.Srv.Service() });
			}
		}

		/// <summary>
		/// Unhandled exception
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		static void CurrentDomain_UnhandledException(object sender, System.UnhandledExceptionEventArgs e)
		{
			try
			{
				// cast
				System.Exception ex = (System.Exception)e.ExceptionObject;

				// log
				EFS.EventLog.Log.LogException(ex);

				// make sal
				new EFS.RC.CC.SAL.Loggers.Messages.Message(null, "CurrentDomain_UnhandledException", ex.Message, "EFS.RC.CC.Srv.Program");
			}
			catch{ }
		}

		/// <summary>
		/// Start server as app
		/// </summary>
		static void RunApp()
		{
			try
			{
				// ASL & CHL
				System.Console.WriteLine(">>>>>>>>>>>>>>>>>>>>>>>>> ASL & SAL & CHL Starting ...");

				// ASL
				EFS.EventLog.Log.StartAsyncLogger(true);

				// CHL
				EFS.RC.CC.CHL.Loggers.Logger.Instance.StartWritter();

				// CHL
				EFS.RC.CC.SAL.Loggers.Logger.Instance.StartWritter();

				// ASL & CHL started
				System.Console.WriteLine(">>>>>>>>>>>>>>>>>>>>>>>>> ASL & SAL & CHL Start COMPLETE");

				// CCC & CCG
				System.Console.WriteLine(">>>>>>>>>>>>>>>>>>>>>>>>> CCC Starting ...");

				// asink start
				EFS.Common.Async.AsyncProcess cccStart = EFS.RC.CC.Platform.Start();

				// wait
				cccStart.WaitComplete();

				// CCC started
				System.Console.WriteLine(">>>>>>>>>>>>>>>>>>>>>>>>> CCC Start COMPETE ");

				// wait input
				System.Console.WriteLine(">>>>>>>>>>>>>>>>>>>>>>>>> Press any key to begin Shutdown");
				System.Console.ReadKey();

				// CCC & CCG
				System.Console.WriteLine(">>>>>>>>>>>>>>>>>>>>>>>>> CCC & CCG Stopping ...");

				// stop
				EFS.Common.Async.AsyncProcess cccStop = EFS.RC.CC.Platform.Stop();

				// wating start
				System.Console.WriteLine(">>>>>>>>>>>>>>>>>>>>>>>>> Wating CCC Stop ...");

				// wait
				cccStop.WaitComplete();

				// wating start
				System.Console.WriteLine(">>>>>>>>>>>>>>>>>>>>>>>>> CCC Stop COMPLETE");

				// wait input
				System.Console.WriteLine(">>>>>>>>>>>>>>>>>>>>>>>>> Press any key to close CHL & ASL & SAL");
				System.Console.ReadKey();

				// wating start
				System.Console.WriteLine(">>>>>>>>>>>>>>>>>>>>>>>>> Wating CHL & ASL & SAL Stop ...");

				// stop CHL log
				EFS.RC.CC.SAL.Loggers.Logger.Instance.StopWritter();

				// stop CHL log
				EFS.RC.CC.CHL.Loggers.Logger.Instance.StopWritter();

				// stop log writter is async
				EFS.EventLog.Log.StopAsyncLogger();

				// wating start
				System.Console.WriteLine(">>>>>>>>>>>>>>>>>>>>>>>>> CHL & ASL & SAL Stoped COMPLETE");

				// wait input
				System.Console.WriteLine(">>>>>>>>>>>>>>>>>>>>>>>>> Press any key to Exit ");
				System.Console.ReadKey();
			}
			catch (System.Exception ex)
			{
				EFS.EventLog.Log.LogException(ex, null);
			}
		}
	}
}
